/*
 * jQuery UI watermark 0.5.0
 *
 * Depends:
 *	ui.core.js
 */
(function($) {

    $.widget("ui.watermark", {

        _init: function() {
            var options = this.options;

			$el = $(this.element);
			var value = $el.val();
			(value == '' || value == options.watermarkText) && $el.val(options.watermarkText).addClass(options.watermarkClass);
			if (options.event == 'keypress') {
				$el.bind('keypress.watermark', this, this._inputKeyPress).bind('keyup.watermark', this, this._inputKeyUp);
			}
			else {
				$el.bind('focus.watermark', this, this._inputFocussed).bind('blur.watermark', this, this._inputBlurred);
			}
        },

        destroy: function() {
			$el = $(this.element);
			$el.unbind('.watermark');
        },
		
		_inputFocussed: function(event) {
			$el = $(this);
			var options = event.data.options, value = $el.val();
			(value == options.watermarkText || value == '') && $el.val('').removeClass(options.watermarkClass);
		},
		
		_inputBlurred: function(event) {
			$el = $(this);
			var options = event.data.options, value = $el.val();
			(value == options.watermarkText || value == '') && $el.val(options.watermarkText).addClass(options.watermarkClass);
		},
		
		_inputKeyPress: function(event) {
			$el = $(this);
			var options = event.data.options, value = $el.val(), key = event.which||((event.charCode||event.charCode===0)?event.charCode:event.keyCode);
			if ((key == null) || (key == 0) || (key == 9) || (key == 13) || (key == 27) || (key == 127)) {
				return true;
			}
			if ((value == options.watermarkText) && (key == 8)) {
				return false;
			}
			(value == options.watermarkText || value == '') && $el.val('').removeClass(options.watermarkClass);
		},
		
		_inputKeyUp: function(event) {
			$el = $(this);
			var options = event.data.options, value = $el.val();
			(value == options.watermarkText || value == '') && $el.val(options.watermarkText).addClass(options.watermarkClass);
		}
    });

	$.extend($.ui.watermark, {
        version: "0.5.0",
        defaults: {
			watermarkClass: 'watermark',
			watermarkText: '',
			event: 'focus'
        },

	    getter: ''
	});
	
})(jQuery);
